<?php


namespace app\api\controller;


use app\common\model\AdminModel;
use app\common\model\GoodslistModel;
use think\Request;

class Admin extends Base
{
    public function userindex(Request $request){
        ///获取分页
        $page = $request->param('page');
        $limit = $request->param('limit');
        $key = $request->param('key');

        $param = $request->param();
        $user = new AdminModel();
        ///判断是否有关键字,如果有关键字，先进行一次筛选
        if(isset($key) && !empty($key)){
            $user = $user->where("username", 'like', '%'.$key.'%');     /// 模糊搜索
        }

        $userinfo = $user->where('role', 3)->page($page, $limit)->select();
        $total = $userinfo->where("role", 3)->count();
        return json(['code'=>1, 'msg'=>'获取所有用户信息成功！', 'data'=>$userinfo, 'total'=>$total]);
    }
    public function userdel(Request $request){
        $id = $request->param('id');
        $user = new AdminModel();
        $userinfo = $user->where('id', $id)->delete();
        if($userinfo){
            return json(['code'=>1, 'msg'=>'删除账号成功']);
        }
        else{
            return json(['code'=>0, 'msg'=>'删除账号失败']);
        }

    }
    public function useredit(Request $request){
        $param = $request->param();
        $user = new AdminModel();
        $userinfo = $user->save($param, ['id'=>$param['id']] );
        if($userinfo){
            return json(['code'=>1, 'msg'=>'更新成功']);
        }
        else{
            return json(['code'=>0, 'msg'=>'更新失败']);
        }
    }

    public function sellindex(Request $request){
        ///获取分页
        $page = $request->param('page');
        $limit = $request->param('limit');
        $key = $request->param('key');

        $param = $request->param();
        $user = new AdminModel();
        $goodslist=new GoodslistModel();
        ///判断是否有关键字,如果有关键字，先进行一次筛选
        if(isset($key) && !empty($key)){
            $user = $user->where("username", 'like', '%'.$key.'%');     /// 模糊搜索
        }
        $userinfo = $user->where('role', 2)->page($page, $limit)->order(['is_recommended'=>'desc'])->select();
        foreach($userinfo as $key => $value)
        {
            $userinfo[$key]['goods_count']=$goodslist->where('busid',$value['id'])->where('status',1)->count();
//            echo $key."<br>";
        }


        $total = $user->where("role", 2)->count();
        return json(['code'=>1, 'msg'=>'获取所有用户信息成功！', 'data'=>$userinfo, 'total'=>$total]);
    }
    public function selldel(Request $request){
        $id = $request->param('id');
        $user = new AdminModel();
        $userinfo = $user->where('id', $id)->delete();
        if($userinfo){
            return json(['code'=>1, 'msg'=>'删除账号成功']);
        }
        else{
            return json(['code'=>0, 'msg'=>'删除账号失败']);
        }
    }
    public function selledit(Request $request){
        $param = $request->param();
        $user = new AdminModel();
        $userinfo = $user->save($param, ['id'=>$param['id']] );
        if($userinfo){
            return json(['code'=>1, 'msg'=>'更新成功']);
        }
        else{
            return json(['code'=>0, 'msg'=>'更新失败']);
        }

    }

}